﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class TahasukaCard : UserControl
    {
        PictureBox picBox = new System.Windows.Forms.PictureBox();
        Tahasuka tahasuka = null;
        public bool IsOpen { get; set; }

        public TahasukaCard()
        {
            InitializeComponent();
            initialize(false);
            picBox.AccessibleName = "מסך ראשי";

            this.Dock = DockStyle.Fill;
        }

        private void changeCard_Click(object sender, EventArgs e)
        {
            Control parentPanel = (Control)sender;
            Control ctrl = (Control)sender;
            ctrl = (Control)ctrl.Tag;

            while (!(parentPanel is SplitContainer))
                parentPanel = parentPanel.Parent;

            SplitContainer split = (SplitContainer)parentPanel;
            if (!split.Panel2.Contains(ctrl))
            {
                split.Panel2.Controls.Clear();
                split.Panel2.Controls.Add(ctrl);
            }
        }

        //default - change the main panel to the picBox
        public void changeMainPanel()
        {
            changeMainPanel(picBox);
            textBoxHeader.Text = string.Empty;
        }
        private void changeMainPanel(Control ctrl)
        {
            if (!splitContainer.Panel2.Contains(ctrl))
            {
                this.splitContainer.Panel2.Controls.Clear();
                this.splitContainer.Panel2.Controls.Add(ctrl);
            }
        }

        //Tahasuka Main events
        private void TahasukaUserControl_Load(object sender, EventArgs e)
        {
            picBox.SizeMode = PictureBoxSizeMode.StretchImage;
            String currentDir = System.IO.Directory.GetCurrentDirectory();
            picBox.Image = Image.FromFile(CompanyManager.StartupPath + @"\images\Pillbox.jpg");
            picBox.Dock = DockStyle.Fill;
            changeMainPanel(picBox);
        }
        private void buttonNewTahasuka_Click(object sender, EventArgs e)
        {
            //case there is an open Tahasuka already
           if (!IsOpen)
           {
               if (buttonNewTahasuka.Tag == null)
                   buttonNewTahasuka.Tag = new TahasukaNewPanel(this);
               changeMainPanel((Control)buttonNewTahasuka.Tag);
           }
           else MessageBox.Show(".ישנו כרטיס תעסוקה פתוח כרגע. לא ניתן לנהל יותר מתעסוקה אחת בו זמנית");
        }
        
        private void buttonOpenCurrent_Click(object sender, EventArgs e)
        {
            if (!IsOpen)
                MessageBox.Show(".אין כרגע כרטיס תעסוקה פתוח");
            else changeMainPanel(tabControl);
            
        }
        private void buttonOpenOld_Click(object sender, EventArgs e) {/* changeMainPanel(mainOldCard); */}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isNewMofa">indicates whether it is a new Mofa or analready active one.</param>
        internal void initialize(bool isNewMofa)
        {
            IsOpen = false;
            SqlCommand selectCmd = SqlManager.SqlCommandFactory("SELECT * FROM TahasukaData WHERE isClosed = 0");

            DataTable dt = SqlManager.selectSqlQueryDataTable(selectCmd);

            //means there is an open Tahasuka already. we allow only 1 Tahasuka to be open at a time.
            if (dt.Rows.Count == 1)
            {
                DataRow row = dt.Rows[0];
                tahasuka = new Tahasuka((DateTime)row["startDate"], (DateTime)row["endDate"], isNewMofa);
                labelTahasukaDetails.Text = (string)row["location"] + "."
                                           + tahasuka.StartDate.ToShortDateString() + "-" + tahasuka.EndDate.ToShortDateString();

                //create all cards in Tahasuka
                buttonNewTahasuka.Tag =  new TahasukaNewPanel(this);
                buttonOpenOld.Tag = new TahasukaOpenOldCard();

                //pre Tahasuka buttons
                buttonZimunim.Tag = new ZimunimCard(tahasuka);
                buttonWorkRequests.Tag = new TahasukaWorkingRequestCard(tahasuka);
                buttonAlal.Tag = new TahasukaAlalCard(tahasuka);
                buttonSiurKav.Tag = new TahasukaSiurKavCard(tahasuka);
                
                //during Tahasuka buttons
                buttonWorkSchedule.Tag = new YamamSchedule(tahasuka);
              //  buttonPostCard.Tag = new TahasukaPostCard(tahasuka);

                //post Tahasuka buttons
                //buttonLekachim.Tag = new LekachimCard();

                changeCard_Click(buttonZimunim, EventArgs.Empty);
                changeCard_Click(buttonWorkSchedule, EventArgs.Empty);
               // changeCard_Click(buttonLekachim, EventArgs.Empty);

                IsOpen = true;
            }
        }
       internal void createNew(SqlCommand sqlCmd)
        {
            //insert the new tahasuka data.
            SqlManager.executeNonQuery(sqlCmd);
            initialize(true);

            tabControl.SelectedIndex = 0;
            changeMainPanel(tabControl);
        }

       override public string ToString()
        {
            return "תעסוקה";
        }

       private void buttonFinish_Click(object sender, EventArgs e)
       {
           string updateStr = @"UPDATE [TahasukaData] SET isClosed=@isClosed WHERE startDate=@startDate";
           SqlCommand updateCmd = SqlManager.SqlCommandFactory(updateStr);
           updateCmd.Parameters.Add("@isClosed", SqlDbType.Bit).Value = true;
           updateCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = tahasuka.StartDate;
           SqlManager.executeNonQuery(updateCmd);
           IsOpen = false;

           labelTahasukaDetails.Text = string.Empty;
       }

    }
}
